package com.hi.hiparty.util;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

@SuppressLint("UseSparseArrays")
public class PictureHandler {
	public static final int REQUEST_USEREDIT_CAMERA = 1001;
	public static final int REQUEST_USEREDIT_ALBUM = 1002;
	public static final int REQUEST_USEREDIT_CROP = 1003; //用于用户编辑裁剪缩放请求满足后处理
	
	public static final int REQUEST_PARTYDETAIL_CAMERA = 1004;
	public static final int REQUEST_PARTYDETAIL_ALBUM = 1005; //用于用户活动留言发图处理
	public static final int RESULT_SUC = 200;
	public static final String TAG = PictureHandler.class.getSimpleName();
	public static Map<Integer,Executor> comMaps = new HashMap<Integer,Executor>();
	
	private PictureHandler(){}
	
	static{
		PictureHandler.getInstance("picHandler");
	}

	public synchronized static PictureHandler getInstance(String name){
		PictureHandler tmp = null;
		if(HipartyApplication.mHandlers.containsKey(name)){
			tmp = HipartyApplication.mHandlers.get(name);
		}else{
			tmp = new PictureHandler();
			HipartyApplication.mHandlers.put(name, tmp);
		}
		return tmp;
	}
	
	public static void register(String name,PictureHandler handler){
		HipartyApplication.mHandlers.put(name, handler);
	}
	
	public void addCommondForCode(int requestCode,Executor command){
		comMaps.put(requestCode, command);
	}
	
	public void startActivityCamera(Activity context,int requestcode){
    	Intent intent = null;
		String state = Environment.getExternalStorageState();
			if (state.equals(Environment.MEDIA_MOUNTED)) {
			    intent = new Intent("android.media.action.IMAGE_CAPTURE");
			    try{
			    	context.startActivityForResult(intent, requestcode);
			    }catch(Exception e){
					Log.v(TAG, "调用相机抛出异常",e);
				}
			} else {
			    Toast.makeText(context,
			            "sdcard无准备", Toast.LENGTH_LONG).show();
			}
	}
	
	public void startActivityAlbum(Activity context,int requestcode){
    	Intent intent = null;
		String state = Environment.getExternalStorageState();
			if (state.equals(Environment.MEDIA_MOUNTED)) {
			    intent = getPhotoPickIntent();
			    context.startActivityForResult(intent, requestcode);
			} else {
			    Toast.makeText(context,
			            "sdcard无准备", Toast.LENGTH_LONG).show();
			}
	}
	
	/**
	 * @param requestCode
	 * @param resultCode
	 * @param data
	 * @param context
	 */
	public void onResult(int requestCode, int resultCode, Intent data,Activity context){
		if(requestCode==REQUEST_USEREDIT_CAMERA&&resultCode==android.app.Activity.RESULT_OK){
			cameraResultHander(data,context);
    	}
    	if(requestCode==REQUEST_USEREDIT_ALBUM&&resultCode==android.app.Activity.RESULT_OK){
    		albumResultHander(data,context);
    	}
		if (requestCode == REQUEST_USEREDIT_CROP
				&& resultCode == android.app.Activity.RESULT_OK) {
			Executor command = comMaps.get(REQUEST_USEREDIT_CROP);
			if (null != command) {
				command.setIntent(data);
				command.setActivity(context);
				command.excute();
			}
		}
    	if(requestCode==REQUEST_PARTYDETAIL_CAMERA&&resultCode==android.app.Activity.RESULT_OK){
    		Bitmap bitmap = getBitMapFromCamere(data);
			data.putExtra("image", bitmap);
			Executor command = comMaps.get(REQUEST_PARTYDETAIL_CAMERA);
			if (null != command) {
				command.setIntent(data);
				command.setActivity(context);
				command.excute();
			}
    	}
    	if(requestCode==REQUEST_PARTYDETAIL_ALBUM&&resultCode==android.app.Activity.RESULT_OK){
    		Bitmap bitmap = getBitmapFromAlbum(data,context);
    		data.putExtra("image", bitmap);
			Executor command = comMaps.get(REQUEST_PARTYDETAIL_CAMERA);
			if (null != command) {
				command.setIntent(data);
				command.setActivity(context);
				command.excute();
			}
    	}
	}
	
	public void cameraResultHander(Intent data,Activity context){
		Bitmap photo=getBitMapFromCamere(data);
		if(null==photo) return;
	    Intent pIntent = getCropImageIntent(photo);
	    context.startActivityForResult(pIntent, REQUEST_USEREDIT_CROP);
	}
	
	Bitmap getBitMapFromCamere(Intent data){
		Bitmap photo=null;
        Uri uri = data.getData();
        if (uri != null) {
        	    photo = BitmapFactory.decodeFile(uri.getPath());
        	}
        if (photo == null) {
        	Bundle bundle = data.getExtras();
        	if (bundle != null) {
        	    photo = (Bitmap) bundle.get("data");
        	    } else {
        	    	Log.v(TAG, "获取照片错误");
        	        return null;
        	    }
        }
        return photo;
	} 
	
	public void albumResultHander(Intent data,Activity context){
        Uri uri = data.getData();
        if(null==uri) return;
   	    Intent pIntent = getCropImageIntent2(uri);
   	    context.startActivityForResult(pIntent, REQUEST_USEREDIT_CROP);
	}
	
	public Bitmap getBitmapFromAlbum(Intent data,Activity activity){
		Uri uri = data.getData();
		  try{  
			// 读取uri所在的图片
			Bitmap bitmap = MediaStore.Images.Media.getBitmap(
					activity.getContentResolver(), uri);
			return bitmap;  
		  }  
		  catch (Exception e){  
			  Log.e(TAG, e.getMessage());  
			  Log.e(TAG, "目录为：" + uri);  
			  return null;  
		  }  
	}
	
	public static Bitmap corpHandler2(Intent data,Activity context){
		Bundle extras = data.getExtras();
		Bitmap bitmap = null;
		if (null != extras) {
			bitmap = extras.getParcelable("data");
		}
		return bitmap;
	}
    //获取裁剪,从相册数据来源，打开jpeg有问题
    public static Intent getCropImageIntent(Bitmap data) {
  	  	Intent intent = new Intent("com.android.camera.action.CROP");
  	  	intent.setType("image/*");
  	  	intent.putExtra("data", data);
  	  	intent.putExtra("crop", "true");
  	  	intent.putExtra("aspectX", 1);
  	  	intent.putExtra("aspectY", 1);
  	  	intent.putExtra("outputX", 128);
  	  	intent.putExtra("outputY", 128);
  	  	intent.putExtra("return-data", true);
  	  	return intent;
  	 }
    
    public static Intent getCropImageIntent2(Uri uri) {
    	Intent intent = new Intent("com.android.camera.action.CROP");
    	intent.setDataAndType(uri, "image/*");
    	intent.putExtra("crop", "true");
    	intent.putExtra("aspectX", 1);
    	intent.putExtra("aspectY", 1);
    	intent.putExtra("outputX", 128);
    	intent.putExtra("outputY", 128);
    	intent.putExtra("return-data", true);
    	return intent;
    }
    /**
     * 调用相册的intent
     * @return
     */
    public static Intent getPhotoPickIntent(){  
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);  
        intent.setType("image/*");  
        return intent;  
    }
    
    
}
